<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/10/19
 * Time: 9:56
 */namespace app\admin\controller;
 use think\Db;
 use app\admin\model\NoticeModel;
 class Notice extends Base
 {
     /*
      * 消息通知首页数据
      */
     public function index(){
         if(request()->isAjax ()){
             extract(input());
             $map = [];
             if(isset($type)&&$type != ""){
                 $map['n_type'] = $type;
             }
             if(isset($app)&&$app != ""){
                 $map['n_app'] = $app;
             }
             if(isset($key)&&$key!="")
             {
                 $map['n_title|n_content'] = ['like',"%" . $key . "%"];
             }
             $map['n_admin'] = 2;
             $Nowpage = input('page') ? input('page'):1;
             $limits = input("limit")?input("limit"):10;// 获取总条数;
             $notice  = new NoticeModel();
             $count = $notice->where($map)->count();
             $lists = $notice->getNoticeData($map,$Nowpage,$limits);
             return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
         }
        return $this->fetch('notice/index');
     }

     /*
      * 添加消息
      */
     public function noticeAdd(){
         if(request ()->isPost ()){
             extract(input());
             $param = [
                 'n_title'=>$n_title,
                 'n_content'=>$n_content,
                 'n_type'=>$n_type,
                 'n_app'=>$n_app,
                 'n_time'=>date('Y-m-d H:i:s')
             ];
             if($n_type == 1){
                 $param['n_read'] = ',0,';
                 $param['n_delete'] = ',0,';
             }else{
                 $param['n_read'] = '0';
                 $param['n_user_id'] = $n_user_id;
             }
             $n = new NoticeModel();
             $res = $n->save($param);
             if($res){
                 //系统和个人消息推送
                 try {
                     if($n_type == 1){
                         sendJpush('',"$n_title",array('a'=>'b'),1,1,$n_app);
                     }else{
                         sendJpush("$n_user_id","$n_title",array('a'=>'b'),2,2,$n_app);
                     }
                 } catch(\Exception $e) {

                 }
                 return json(['code'=>200,'msg'=>'添加成功']);
             }else{
                 return json(['code'=>100,'msg'=>'添加失败']);
             }
         }
         return $this->fetch('notice/notice_add');
     }


     public function userData(){
         extract(input());
         if($type == 2){
             switch($user){
                 case 1:
                     $data = Db::name('u_user')->where('user_status',1)->field('user_id id,user_username name')->select();
                     break;
                 case 2:
                     $data = Db::name('shops')->where('s_status',1)->field('s_id id,s_name name')->select();
                     break;
                 case 3:
                     $data = Db::name('work_user')->where(['user_status'=>1,'user_agree'=>2])->field('user_id id,user_name name')->select();
                     break;
             }
         }
         return json(['code'=>200,'msg'=>$data]);
     }

     /*
      * 删除消息
      */
     public function noticeDel(){
         $res = NoticeModel::destroy (input('id'));
         if($res){
             return json(['code'=>200,'msg'=>'删除成功']);
         }else{
             return json(['code'=>100,'msg'=>'删除失败']);
         }
     }

     public function topNotice(){
         extract(input());
         $notice = new NoticeModel();
         $res = $notice->save(['n_top'=>$num],['n_id'=>$id]);
         if($res){
             return json(['code'=>200,'msg'=>'']);
         }else{
             return json(['code'=>100,'msg'=>'']);
         }
     }
 }